// pages/cupdetail/cupdetail.js
const app = getApp();
import { u_formatTimestamp } from '../../config/tool.js';
import { seckill } from '../../utils/util.js';
import { vipReceived } from '../../config/https.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '', // 订单id
    orderInfo: '', // 订单信息
    totalGoodsPrice: 0, // 商品总价
    showed: false, // 是否展示弹窗
    getList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        id: options.id
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getData();
  },

  // 获取数据
  getData() {
    app.loading();
    let that = this;
    wx.request({
      url: app.baseURL + 'order/getPackageDetail',
      data: {
        token: app.globalData.userId,
        order_id: that.data.id,
      },
      success(data) {
        let res = data.data;
        wx.hideLoading();
        if (res.code == 1) {
          if(res.data.info.is_status == 1){
            that.getHis();
          }else{
            that.getRecive();
          }

          // 获取商品总价
          res.data.info.goods.forEach((item) => {
            that.setData({
              totalGoodsPrice: (Number((Number(item.total_price) * Number(item.total_num)).toFixed(2)) + Number(that.data.totalGoodsPrice)).toFixed(2)
            })
          })

          // 改变时间格式
          if (res.data.info.createtime) {
            res.data.info.createtime = u_formatTimestamp(res.data.info.createtime * 1000);
          }
          if (res.data.info.freight_time) {
            res.data.info.freight_time = u_formatTimestamp(res.data.info.freight_time * 1000);
          }
          if (res.data.info.pay_time) {
            res.data.info.pay_time = u_formatTimestamp(res.data.info.pay_time * 1000);
          }

          that.setData({
            orderInfo: res.data.info
          })
        } else {
          app.$tip(res.msg);
        }
      }
    })
  },

  // 获取发货记录
  getHis() {
    let that = this;
    app.loading();
    wx.request({
      url: app.baseURL + 'order/getDeliveryRecord',
      data: {
        token: app.globalData.userId,
        order_id: that.data.id
      },
      success(data) {
        let res = data.data;
        wx.hideLoading();
        if (res.code == 1) {
          if (res.data.list.length > 0) {
            res.data.list.forEach((item) => {
              item.create_time = u_formatTimestamp(item.create_time * 1000)
            })
            
            that.setData({
              getList: res.data.list,
            })
          }
        } else {
          app.$tip(res.msg);
        }
      }
    })
  },

  // 获取提货记录
  getRecive() {
    let that = this;
    wx.request({
      url: app.baseURL + 'order/getWriteOffRecord',
      data: {
        token: app.globalData.userId,
        order_id: that.data.id
      },
      success(data) {
        let res = data.data;
        if (res.code == 1) {
          if (res.data.list.length > 0) {
            res.data.list.forEach((item) => {
              item.create_time = u_formatTimestamp(item.create_time * 1000)
            })
            that.setData({
              getList: res.data.list,
            })
          }
        } else {
          app.$tip(res.msg);
        }
      }
    })
  },

  // 展示弹窗
  showList(){
    if (this.data.getList.length > 0){
      this.setData({
        showed: !this.data.showed
      })
    }else{
      if(this.data.orderInfo.is_status == 1){
        app.$tip('暂时没有发货记录哦~')
      }else{
        app.$tip('暂时没有提货记录哦~')
      }
    }
  },

  // 确认收货
  recived(e) {
    let id = e.currentTarget.dataset.id, that = this;
    wx.showModal({
      title: '温馨提示',
      content: '您确定要确认收货吗?',
      confirmColor: '#5CA032',
      success(res) {
        if (res.confirm) {
          app.loading();
          vipReceived({
            id: id,
            token: app.globalData.userId
          }).then(ress => {
            wx.hideLoading();
            if (ress.code == 1) {
              app.$tip(ress.msg)
              setTimeout(() => {
                that.getData();
              }, 1500)
            } else {
              app.$tip(ress.msg)
            }
          })
        }
      }
    })
  },

  // 去提货
  toRecive(e) {
    wx.navigateTo({
      url: '/pages/receiveCode/receiveCode?id=' + e.currentTarget.dataset.id,
    })
  },

  // 查看物流
  toLogistics(e) {
    let orderid = e.currentTarget.dataset.order_id, id = e.currentTarget.dataset.id;
    if(orderid){
      wx.navigateTo({
        url: '/pages/logistics/logistics?id=' + id + '&order_id=' + orderid,
      })
    }else{
      wx.navigateTo({
        url: '/pages/logistics/logistics?order_id=' + id,
      })
    }
  },

  // 拨打电话
  makeCall() {
    wx.makePhoneCall({
      phoneNumber: this.data.orderInfo,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})